package color.koitq.picker.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.permissions.Permission;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.quexin.pickmedialib.PickerMediaContract;
import com.quexin.pickmedialib.PickerMediaParameter;
import com.quexin.pickmedialib.PickerMediaResutl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import color.koitq.picker.R;
import color.koitq.picker.activty.HomeColorListActivity;
import color.koitq.picker.activty.ImageColorActivity;
import color.koitq.picker.ad.AdFragment;
import color.koitq.picker.adapter.HomeAdapter;
import color.koitq.picker.decoration.GridSpaceItemDecoration;
import color.koitq.picker.entity.HomeModel;
import color.koitq.picker.entity.PaletteModel;
import color.koitq.picker.util.MyPermissionsUtils;
import color.koitq.picker.util.SQLdm;

public class HomeFrament extends AdFragment {

    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.list)
    RecyclerView list;
    private HomeAdapter mAdapter;
    private View mView;
    private int mId = -1;
    private ActivityResultLauncher<PickerMediaParameter> pickerMeida;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_ui;
    }

    @Override
    protected void init() {
        topbar.setTitle("色卡");
        initList();
    }

    @OnClick(R.id.pen)
    public void onClick(View view) {
        MyPermissionsUtils.requestPermissionsTurn(requireActivity(), new MyPermissionsUtils.HavePermissionListener() {
            @Override
            public void havePermission() {
                mView = view;
                showVideoAd();
            }
        }, Permission.MANAGE_EXTERNAL_STORAGE);

    }

    private void initList() {
        list.setLayoutManager(new GridLayoutManager(getContext(), 1));
        list.addItemDecoration(new GridSpaceItemDecoration(1, QMUIDisplayHelper.dp2px(getContext(), 30),
                QMUIDisplayHelper.dp2px(getContext(), 0)));
        mAdapter = new HomeAdapter();
        list.setAdapter(mAdapter);
        mAdapter.setHomeItemClickListener(new HomeAdapter.HomeItemClickListener() {
            @Override
            public void click(int id) {
                mId = id;
                Log.d("TAG", "click: "+id);
                showVideoAd();
            }
        });

        pickerMeida = registerForActivityResult(new PickerMediaContract(), new ActivityResultCallback<PickerMediaResutl>() {
            @Override
            public void onActivityResult(PickerMediaResutl result) {
                if (result.isPicker()) {
                    Intent intent = new Intent(getContext(), ImageColorActivity.class);
                    intent.putExtra("path",result.getResultData().get(0).getPath());
                    startActivity(intent);
                }
            }
        });
        loadData();
    }

    private void loadData() {
        List<HomeModel> homeModels = new ArrayList<>();
        List<PaletteModel> tempList = new ArrayList<>();
        ArrayList<PaletteModel> paletteModels = SQLdm.queryHomeColors();
        int type = 0;
        for (PaletteModel paletteModel : paletteModels) {
            tempList.add(paletteModel);
            if (tempList.size() >= 2) {
                type++;
                HomeModel homeModel = new HomeModel();
                homeModel.mModels = tempList;
                homeModel.type = type;
                homeModels.add(homeModel);
                if (type == 2) {
                    type = 0;
                }
                tempList = new ArrayList<>();
            }

        }
        mAdapter.setNewInstance(homeModels);
    }

    @Override
    protected void fragmentAdClose() {
        topbar.post(() -> {
            if (mView != null) {
                pickerMeida.launch(new PickerMediaParameter().picture().requestCode(1));
            } else if (mId != -1) {
                Intent intent  = new Intent(getContext(), HomeColorListActivity.class);
                intent.putExtra("id",mId);
                startActivity(intent);
            }
            mView = null;
            mId = -1;
        });
    }
}
